###传入导出服务器及用户信息
export_db_ip=$1   # 服务器ip地址
export_db_port=$2  # 服务器端口号
export_user=$3     # 用户名
export_user_passwd=$4  # 用户密码

###文件保存目录
path=`pwd` #当前目录

mysql -B -N -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -e "SELECT CONCAT('\'', user,'\'@\'', host, '\'') FROM user WHERE user != 'mysql.session' and user != 'mysql.sys' AND user != 'root' AND user != ''" mysql > $path/mysql_all_users.txt

###导出用户权限
while read line; 
do 
   mysql -B -N -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -e "SHOW GRANTS FOR $line"; 
done < $path/mysql_all_users.txt > $path/mysql_all_users_sql.sql

###每行行尾增加；
sed -i 's/$/;/' $path/mysql_all_users_sql.sql

###立即生效
echo "flush privileges;" >> $path/mysql_all_users_sql.sql